/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * editarCotizacion_Descripcion.java
 *
 * Created on 06-jun-2011, 10:22:15
 */

package vista.planificacion;

import controlador.planificacion.GestorEditarTarea;
import controlador.planificacion.GestorPlanificacionSubTareas;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import util.*;
import vista.interfaces.ICallBackGen;

/**
 *
 * @author Administrador
 */
public class PlanificacionSubTareas extends javax.swing.JPanel implements ICallBackGen {

    private GestorPlanificacionSubTareas gestor;
    
    public static final String CALLBACK_NUEVASUBTAREA  = "NUEVASUBTAREA";
    
    /** Creates new form editarCotizacion_Descripcion */
    public PlanificacionSubTareas(GestorPlanificacionSubTareas gestor) {
        initComponents();
        this.gestor = gestor;
        
        gestor.setPantalla(this);
        gestor.cargarSubTareas();
        
        cargarTareaContenedora();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     *
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        lblTareaContenedora = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaSubTareas = new javax.swing.JTable();
        btnNuevaSubTarea = new javax.swing.JButton();
        btnEditarSubTarea = new javax.swing.JButton();
        btnEliminarSubTarea = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tarea Contenedora", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel2.setText("La tarea actual está siendo contenida por la tarea:");

        lblTareaContenedora.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblTareaContenedora.setText("Ninguna, no tiene padre");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblTareaContenedora, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTareaContenedora)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Listado de SbuTareas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tablaSubTareas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Sub Tarea", "Fecha Inicio", "Fecha Fin"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaSubTareas);
        tablaSubTareas.getColumnModel().getColumn(0).setResizable(false);
        tablaSubTareas.getColumnModel().getColumn(0).setPreferredWidth(100);
        tablaSubTareas.getColumnModel().getColumn(1).setResizable(false);
        tablaSubTareas.getColumnModel().getColumn(2).setResizable(false);

        btnNuevaSubTarea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/add.png"))); // NOI18N
        btnNuevaSubTarea.setText("Nueva Sub Tarea");
        btnNuevaSubTarea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaSubTareaActionPerformed(evt);
            }
        });

        btnEditarSubTarea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/Modify.png"))); // NOI18N
        btnEditarSubTarea.setText("Editar");
        btnEditarSubTarea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarSubTareaActionPerformed(evt);
            }
        });

        btnEliminarSubTarea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/Erase.png"))); // NOI18N
        btnEliminarSubTarea.setText("Eliminar");
        btnEliminarSubTarea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarSubTareaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(btnNuevaSubTarea)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, Short.MAX_VALUE)
                .addComponent(btnEliminarSubTarea)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditarSubTarea))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevaSubTarea)
                    .addComponent(btnEditarSubTarea)
                    .addComponent(btnEliminarSubTarea)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditarSubTareaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarSubTareaActionPerformed
        GestorEditarTarea gestorEditarTarea = new GestorEditarTarea(this.gestor.getGestorPlanificacion());
        DefaultTableModel dtm = (DefaultTableModel) this.tablaSubTareas.getModel();
        Tupla t = (Tupla)dtm.getValueAt(tablaSubTareas.getSelectedRow(),0);
        gestorEditarTarea.seleccionarTarea(this.gestor.getSubTareaPlanificada(t.getId())); // Editando una tarea existente en memoria
        gestorEditarTarea.setGestorTareaPadre(this.gestor.getGestorPadre());
        PantallaEditarTarea editarTarea = new PantallaEditarTarea(gestorEditarTarea,this.gestor.getGestorPadre().getPantalla());
        SwingPanel.getInstance().addWindow(editarTarea);
        editarTarea.setVisible(true);
    }//GEN-LAST:event_btnEditarSubTareaActionPerformed

    private void btnNuevaSubTareaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaSubTareaActionPerformed
        GestorEditarTarea gestorEditarTarea = new GestorEditarTarea(this.gestor.getGestorPlanificacion());
        gestorEditarTarea.seleccionarTarea(0); // Una tarea nueva
        gestorEditarTarea.setGestorTareaPadre(this.gestor.getGestorPadre());
        PantallaEditarTarea editarTarea = new PantallaEditarTarea(gestorEditarTarea,this.gestor.getGestorPadre().getPantalla());
            // Seteo El Callback
            editarTarea.setCallback(this);
        SwingPanel.getInstance().addWindow(editarTarea);
        editarTarea.setVisible(true);
    }//GEN-LAST:event_btnNuevaSubTareaActionPerformed

    private void btnEliminarSubTareaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarSubTareaActionPerformed
       GestorEditarTarea gestorEditarTarea = new GestorEditarTarea(this.gestor.getGestorPlanificacion());
       gestorEditarTarea.setGestorTareaPadre(this.gestor.getGestorPadre());
       DefaultTableModel dtm = (DefaultTableModel) this.tablaSubTareas.getModel();
       Tupla t = (Tupla)dtm.getValueAt(tablaSubTareas.getSelectedRow(),0);
       gestorEditarTarea.seleccionarTarea(this.gestor.getSubTareaPlanificada(t.getId()));
       
       StringBuilder mensajeRetorno = new StringBuilder();
       if(gestorEditarTarea.eliminarComoSubTarea(mensajeRetorno))
       {
           JOptionPane.showMessageDialog(new JFrame(),mensajeRetorno,"Exito!",JOptionPane.INFORMATION_MESSAGE);
       }
       else
       {
           JOptionPane.showMessageDialog(new JFrame(),mensajeRetorno,"Error!",JOptionPane.INFORMATION_MESSAGE);
       }
       // Refrescar Tabla
       cargarSubTareas();
    }//GEN-LAST:event_btnEliminarSubTareaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditarSubTarea;
    private javax.swing.JButton btnEliminarSubTarea;
    private javax.swing.JButton btnNuevaSubTarea;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblTareaContenedora;
    private javax.swing.JTable tablaSubTareas;
    // End of variables declaration//GEN-END:variables

    public void cargarSubTareas() {
        DefaultTableModel dtm = (DefaultTableModel) tablaSubTareas.getModel();
        TablaUtil.vaciarDefaultTableModel(dtm);
        Iterator<NTupla> itSubTareas = gestor.getSubTareas().iterator();
        while(itSubTareas.hasNext())
        {
            NTupla nt = itSubTareas.next();
            Date[] fechas = (Date[])nt.getData();
            Tupla t = new Tupla();
            t.setNombre(nt.getNombre());
            t.setId(nt.getId());
            String fechaInicio = "";
            if(fechas[0] != null)
                fechaInicio = FechaUtil.getFecha(fechas[0]);
            String fechaFin = "";
            if(fechas[1] != null)
                fechaFin = FechaUtil.getFecha(fechas[1]);
            Object[] fila = { t,fechaInicio,fechaFin};
            dtm.addRow(fila);
        }
    }

    public void actualizarSubTareas() {
        this.cargarSubTareas();
    }

    @Override
    public void actualizar(int id, String flag, Class tipo) {
        
        if(flag.equals(PlanificacionSubTareas.CALLBACK_NUEVASUBTAREA))
        {
            // Terminó de agregar una nueva SubTarea por lo tanto actualizo la tabla
            actualizarSubTareas();
        }
            
        
    }

    private void cargarTareaContenedora() {
        String nombre = gestor.getNombreTareaContenedora();
        lblTareaContenedora.setText(nombre);
    }
    
    
}
